#!/bin/bash

export CLASS_FILE=$(find . -name "SimTargetImpl.class" | head -n 1)
export TARGET_FILE=/tmp/SimTargetBytes.java

function dump_bytes() {
    hexdump -C $CLASS_FILE  | \
    sed "/^.\{8\}$/d"       | \
    sed "s/^.\{10\}//g"     | \
    sed "s/  |.*$//g"       | \
    sed "s/  / /g"          | \
    sed "s/ \{2,\}//g"      | \
    sed "s/ /,0x/g"         | \
    sed "s/^/0x/g"          | \
    sed "s/0x/(byte)0x/g"   | \
    sed "s/,/, /g"          | \
    sed "s/$/,/g"           | \
    sed "s/^/        /g"
}

rm -rf $TARGET_FILE

{
    echo "// Auto generated by class2bytes.sh";
    echo "// Do not edit by hands";
    echo "// Java class bytecode, JDK8";
    echo "";
    echo "// SimTargetBytes.java, generated from $CLASS_FILE";
    echo "package demo.base;";
    echo "";
    echo "public final class SimTargetBytes {";
    echo "";
    echo "    public static final byte[] SIM_TARGET_BYTES = {";
    dump_bytes;
    echo "    };";
    echo "";
    echo "    private SimTargetBytes(){";
    echo "    }";
    echo "";
    echo "}";
} > $TARGET_FILE

cp $TARGET_FILE demo-inmem-jdk8/src/main/java/demo/base/SimTargetBytes.java
cp $TARGET_FILE demo-inmem-jdk11/src/main/java/demo/base/SimTargetBytes.java
cp $TARGET_FILE demo-inmem-jdk17/src/main/java/demo/base/SimTargetBytes.java

